﻿using DAL;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WebYuekaoUI.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoomController : ControllerBase
    {
        public RoomController(IRoomBll roomBll,IWebHostEnvironment web,ILogger<RoomController> logger)
        {
            RoomBll = roomBll;
            Web = web;
            Logger = logger;
        }

        public IRoomBll RoomBll { get; }
        public IWebHostEnvironment Web { get; }
        public ILogger<RoomController> Logger { get; }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file">上传文件格式</param>
        /// <returns>返回图片路由</returns>
        [HttpPost]
        public IActionResult Up(IFormFile file)
        {
            try//异常处理
            {
                Logger.LogInformation("记录日志");
                string path = Web.WebRootPath + @"\img\";
                FileStream fa = System.IO.File.Create(path + file.FileName);
                file.CopyTo(fa);
                fa.Close();
                return Ok("/img/" + file.FileName);
            }
            catch (Exception)
            {
                Logger.LogError("记录错误日志");
                throw;
            }
        }

        /// <summary>
        /// 居民信息录入
        /// </summary>
        /// <param name="room">表名</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public Task<int> Tian(Room room)
        {
            try//异常处理
            {
                Logger.LogInformation("记录日志");
                return RoomBll.Tian(room);
            }
            catch (Exception)
            {
                Logger.LogError("记录错误日志");
                throw;
            }
        }
        /// <summary>
        ///  显示居民信息
        /// </summary>
        /// <param name="name">居民姓名</param>
        /// <param name="types">居民类型</param>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">页容量</param>
        /// <returns></returns>
        [HttpGet]
        public dynamic Xian(string? name, string? types, int PageIndex, int PageSize)
        {
            try//异常处理
            {
                Logger.LogInformation("记录日志");
                return RoomBll.Xian(name, types, PageIndex, PageSize);
            }
            catch (Exception)
            {
                Logger.LogError("记录错误日志");
                throw;
            }
        }
        /// <summary>
        /// 批量删除功能（逻辑删除）
        /// </summary>
        /// <param name="sid">sid标识符</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public Task<int> PlLjDelete(string sid)
        {
            try//异常处理
            {
                Logger.LogInformation("记录日志");
                return RoomBll.PlLjDelete(sid);
            }
            catch (Exception)
            {
                Logger.LogError("记录错误日志");
                throw;
            }
        }
        /// <summary>
        /// 批量审核功能（修改状态为已审核）
        /// </summary>
        /// <param name="sid">sid标识符</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public Task<int> PlShUpdate(string sid)
        {
            try//异常处理
            {
                Logger.LogInformation("记录日志");
                return RoomBll.PlShUpdate(sid);
            }
            catch (Exception)
            {
                Logger.LogError("记录错误日志");
                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="room">表名</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public Task<int> Xiu(Room room)
        {
            try//异常处理
            {
                Logger.LogInformation("记录日志");
                return RoomBll.Xiu(room);
            }
            catch (Exception)
            {
                Logger.LogError("记录错误日志");
                throw;
            }
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id">id标识符</param>
        /// <returns></returns>
        [HttpGet]
        public Room Fan(int id)
        {
            try//异常处理
            {
                Logger.LogInformation("记录日志");
                return RoomBll.Fan(id);
            }
            catch (Exception)
            {
                Logger.LogError("记录错误日志");
                throw;
            }
        }
        /// <summary>
        /// 记录错误日志
        /// </summary>
        [HttpGet]
        public void Err()
        {
            try//异常处理
            {
                Logger.LogInformation("记录日志");
                int j = 0;
                int k = 0;
                int l = j / k;
            }
            catch (Exception)
            {
                Logger.LogError("记录错误日志");
                throw;
            }
        }
    }
}